<template>
  <div class="deviceManage_list">
    <one-title :title="titleText" @refreshData="refresh"></one-title>
    <div class="fixed_top_56">
      <mes-search-comp
        :searchList="searchList"
        ref="mesSearch"
        :searchResult="searchResult"
        @searchInfo="_info">
      </mes-search-comp>
      <div class="table_container">
        <el-table
          :data="listData.list"
          ref="tableNode"
          border
          stripe
          v-loading="isLoading"
          style="width: 100%"
          tooltip-effect="dark"
          align="center"
          @sort-change="_sort"
          :max-height="tableMaxHeight"
          @row-click="(row) => $refs.tableNode.toggleRowSelection(row)"
          @selection-change="handleSelectionChange">
          <mes-col-comp
            :key="index"
            v-for="(column, index) in tableColumns"
            :column="column"
          ></mes-col-comp>
          <el-table-column
            prop="templateName"
            :resizable="false"
            label="操作"
            min-width="80"
            show-overflow-tooltip
            fixed="right"
            align="center">
            <template slot-scope="scope">
              <span class="jump_item"
                    v-if="authorityJudge(scope.row)"
                    :class="{is_disabled: !scope.row.parameterRecordId}"
                    @click="showRecord(scope.row)">参数记录</span>
              <span class="jump_item is_disabled"
                    v-else>暂无权限</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <mes-pagination
        @updateData="init"
        :currentPage="listData.pageNo"
        :total="listData.count || 0"
        :pageSize="listData.pageSize">
      </mes-pagination>
      <see-records ref="SeeRecords"></see-records>
    </div>
  </div>
</template>

<script>
import {InspectionType} from '@/components/mesDict'
import {SeeRecords} from '@/components/Template'
import tableColumns from './tableColumns'

export default {
  name: 'HistoricalInspection',
  components: {
    InspectionType,
    SeeRecords
  },
  data() {
    return {
      titleText: '历史检验数据查询', // 模块标题
      selectData: [], // 选中的表格数据
      searchData: {}, // 搜索列表数据
      searchResult: NaN, // 搜索结果数
      isLoading: false, // 加载状态
      sortData: {}, // 排序对象
      tableColumns: tableColumns, // 列表数组
    }
  },
  computed: {
    // 获取列表数据
    listData() {
      return this.$store.state.historicalInspection.listData
    },
    userInfo() {
      return this.$store.state.userInfo || {}
    },
    // 专检id
    inspectionId() {
      let list = this.$store.state.dict.inspectionType.labels || []
      let item = list.find((item) => item.value === 'Special') || {id: ''}
      return item.id
    },
    // 工序进度“已完成”状态id
    workstageProcessStatusId() {
      let list = this.$store.state.dict.workstageProcessStatusType.labels || []
      let item = list.find((item) => item.value === 'Finished') || {id: ''}
      return item.id
    },
    // 人员列表查询条件
    searchList() {
      let searchList = this.tableColumns || []
      return searchList
    },
  },
  mounted() {
    this.init()
  },
  methods: {
    // 刷新方法
    refresh() {
      this.$refs.mesSearch.clean()
      this.$refs.tableNode.clearSort()
      this.searchData = {}
      this.sortData = {}
      this.init()
    },
    // 格式化参数
    formatPostData(page, parameter) {
      let parameterArr = parameter || []
      return {
        pageNo: page ? page.pageNo : 1,
        pageSize: page ? page.pageSize : this.listData.pageSize || 15,
        sort: this.sortData,
        query: {
          columns: this.tableColumns.reduce((pre, cur) => [...pre, cur.prop], [...parameterArr]),
          inspectionTypeId: this.inspectionId,
          workstageProcessStatusId: this.workstageProcessStatusId,
          ...this.searchData
        }
      }
    },
    // 初始化
    init(page) {
      this.isLoading = true
      let postData = this.formatPostData(page, ['id', 'parameterRecordId', 'paramRecordSecretLevelId'])
      this.$store.dispatch('saveData', {
        method: 'post',
        url: 'a/turnoverworkstage/turnoverWorkstage/list',
        data: postData,
        module: 'historicalInspection',
        state: 'listData'
      }).then((res) => {
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 查询
    _info(data) {
      this.$refs.tableNode.clearSort()
      this.sortData = {}
      this.searchData = data
      this.init()
    },
    // 显示参数记录
    showRecord(row) {
      if (row && row.parameterRecordId) this.$refs.SeeRecords.openDialog(row.id)
    },
    // 多选表格数据
    handleSelectionChange(val) {
      this.selectData = val
    },
    // 排序
    _sort(obj) {
      this.sortData = this.$getSortOrder(obj)
      this.init()
    },
    // 权限判断
    authorityJudge(row) {
      switch (this.userInfo.secretLevel) {
        case 'ID_PSL_Confidential':
          return true
        case 'ID_PSL_Secret':
          return row.paramRecordSecretLevelId === 'ID_PSL_Public' || row.paramRecordSecretLevelId === 'ID_PSL_Secret'
        case 'ID_PSL_Public':
          return row.paramRecordSecretLevelId === 'ID_PSL_Public'
        default:
          return false
      }
    }
  }
}
</script>

<style scoped lang="less">
</style>
